<?php

//~ 载入QQ登陆类文件
require get_template_directory() . '/connect/login.qq.class.php';
//~ 载入微博登陆类文件
require get_template_directory() . '/connect/login.sina.class.php';

/**
 * QQ登录类
 */
new LoginQqClass();

/**
 * 新浪微博登录类
 */
new LoginSinaClass();

session_start();
if (!function_exists('wp_get_current_user')) {
	include (ABSPATH . "wp-includes/pluggable.php");
}

//~ 增加QQ头像，微博头像
add_filter('get_avatar', 'add_avatar', 1, 5);
function add_avatar($avatar, $id_or_email, $size, $default, $alt) {
	if ($id_or_email != '') {
		if (is_numeric($id_or_email)) {
			$id = (int)$id_or_email;
			$username = get_user_meta($id, 'nickname', 1);
		} elseif (is_object($id_or_email)) {
			if (!empty($id_or_email -> comment_type) && !in_array($id_or_email -> comment_type, (array)$allowed_comment_types))
				return false;
			if (!empty($id_or_email -> user_id)) {
				$id = (int)$id_or_email -> user_id;
			}
			$username = get_user_meta($id, 'nickname', 1);
		} else {
			$email = $id_or_email;
			$user = get_user_by('email', $email);
			$id = $user -> ID;
			$username = $user -> user_nicename;
		}
		$qquserimg = get_user_meta($id, 'qquserimg', true);
		$sinauserimg = get_user_meta($id, 'sinauserimg', true);
		if ($alt == '') {
			$alt = $username;
		}
		if ($qquserimg == '' && $sinauserimg == '') {
			return $avatar;
		} elseif ($qquserimg != '') {
			$avatar = '<img width="' . $size . '" height="' . $size . '" class="avatar" src="' . $qquserimg . '" alt="' . $alt . '">';
			return $avatar;
		} elseif ($sinauserimg != '') {
			$avatar = '<img width="' . $size . '" height="' . $size . '" class="avatar" src="' . $sinauserimg . '" alt="' . $alt . '">';
			return $avatar;
		}
	}
	return $avatar;
}

//~ CURL POST请求
if (!function_exists('do_post')) {
	function do_post($url, $data) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		$ret = curl_exec($ch);
		curl_close($ch);
		return $ret;
	}

}

//~ 请求url地址
if (!function_exists('get_url_contents')) {
	function get_url_contents($url) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_URL, $url);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}

}
//~ 创建数据库字段，在user表中增加一个`openid`和 `uid`两个字段
add_action('activated_plugin', 'plugin_activation');
if (!function_exists('plugin_activation')) {
	function plugin_activation() {
		global $wpdb;
		// 创建openid字段(QQ)用于记录QQ返回的用户openid
		$var = $wpdb -> query("SELECT openid FROM $wpdb->users");
		if (!$var) {
			$wpdb -> query("ALTER TABLE $wpdb->users ADD openid varchar(50)");
		}
		// 创建uid字段(新浪)用于记录新浪微博返回的用户uid
		$var1 = $wpdb -> query("SELECT uid FROM $wpdb->users");
		if (!$var1) {
			$wpdb -> query("ALTER TABLE $wpdb->users ADD uid varchar(50)");
		}
	}

}

//~ 构造登录链接
if (!function_exists('login_url')) {
	function login_url($type, $rurl = '') {
		global $wp;
		if (empty($rurl)) {
			$url = home_url(add_query_arg(array()));
		} else {
			$url = $rurl;
		}
		$login_url = home_url("/") . '?connect=' . $type . '&r=' . $url;
		return $login_url;
	}

}
//~ 自定义登录框内容
add_action('login_form', 'admin_custom_login');
if (!function_exists('admin_custom_login')) {
	function admin_custom_login() {
		echo '<a class="connect qq" href="' . login_url('qq', home_url()) . '">QQ账号登录</a>';
		echo '<a class="connect sina" href="' . login_url('sina', home_url()) . '">新浪微博登录</a>';
	}

}

//~ 增加用户信息字段
add_filter('user_contactmethods', 'user_contact_methods');
function user_contact_methods($user_contact) {
	$user_contact['qquserimg'] = 'QQ头像';
	$user_contact['sinauserimg'] = '新浪头像';
	return $user_contact;
}

//~ 判断是否绑定
function is_bind($type) {
	global $wpdb;
	if ($type == 'qq') {
		$field = 'openid';
	} elseif ($type == 'sina') {
		$field = 'uid';
	}
	$user_ID = get_current_user_id();
	$query = "SELECT {$field} FROM `{$wpdb->users}` where ID='{$user_ID}'";
	$openid_db = $wpdb -> get_var($query);
	if ($openid_db)
		return $openid_db;
	else
		return false;
}

//绑定
add_action('show_user_profile', 'bind');
function bind() {
	echo '<table class="form-table"><tr><th scope="row"><label>第三方账号绑定</label></th><td>';
	if (is_bind('qq')) {
		echo '<a class="button" href="',  add_query_arg('connect', 'qqunwrap', get_edit_user_link()), '">解绑QQ账号</a>　';
	} else {
		echo '<a class="button" href="',  login_url('qqbind', get_edit_user_link()), '">绑定QQ账号</a>　';
	}
	if (is_bind('sina')) {
		echo '<a class="button" href="',  add_query_arg('connect', 'sinaunwrap', get_edit_user_link()), '">解绑新浪微博账号</a>';
	} else {
		echo '<a class="button" href="',  login_url('sinabind', get_edit_user_link()), '">绑定新浪微博账号</a>';
	}
	echo '</td></tr></table>';
}
?>